package com.example.no1admin.service.impl;

import com.example.no1admin.ex.ServiceException;
import com.example.no1admin.mapper.OrderMapper;
import com.example.no1admin.pojo.dto.order.OrderAddNewDTO;
import com.example.no1admin.pojo.dto.order.OrderEditDTO;
import com.example.no1admin.pojo.entity.Order;
import com.example.no1admin.pojo.vo.order.OrderDetailVO;
import com.example.no1admin.pojo.vo.order.OrderListItemVO;
import com.example.no1admin.service.IOrderService;
import com.example.no1admin.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
@Slf4j
public class OrderServiceImpl implements IOrderService {
    
    @Autowired
    OrderMapper orderMapper;

    public OrderServiceImpl() {
        log.debug("创建业务逻辑对象：OrderServiceImpl");
    }

    @Override
    public void addNew(OrderAddNewDTO orderAddNewDTO) {
        log.debug("准备开始处理创建订单的业务，参数：{}",orderAddNewDTO);
        //根据userId判断用户订单是否存在
        Long userId = orderAddNewDTO.getUserId();
        int count = orderMapper.countByUserId(userId);
        if(count > 0){
            String message = "创建订单失败，用户订单id【"+userId+"】已经存在！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        //创建实体对象(mapper的方法参数是实体类型)
        Order order = new Order();
        //将当前方法参数的值复制到Brand实体类型的对象中
        BeanUtils.copyProperties(orderAddNewDTO,order);
        //补全属性值(不由客户端提交的属性的值，应该在插入之前补全)

        //将订单数据写入到数据库
        log.debug("即将向表中插入数据：{}",order);
        int rows = orderMapper.insert(order);
        if(rows!=1){
            String message = "创建订单失败，服务器忙，请稍后再试!";
            log.error(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }

    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理删除订单的业务,id={}",id);
        OrderDetailVO order = orderMapper.getById(id);
        if(order==null){
            String message = "删除订单失败，尝试删除的数据【"+id+"】不存在！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int rows = orderMapper.deleteById(id);
        if(rows!=1){
            String message = "删除订单失败，服务器忙，请稍后再试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    @Override
    public void deleteByIds(Long... ids) {
        log.debug("开始处理删除订单的业务,ids={}",ids);
        int rows = orderMapper.deleteByIds(ids);
    }

    @Override
    public void updateById(OrderEditDTO orderEditDTO) {
        //将订单数据写入到数据库
        log.debug("即将向表中插入需要修改的数据：{}",orderEditDTO);
        int rows = orderMapper.updateById(orderEditDTO);
        if(rows!=1){
            String message = "修改订单失败，服务器忙，请稍后再试!";
            log.error(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }

    }

    @Override
    public OrderDetailVO getById(Long id) {
        log.debug("开始处理查询订单的业务,id={}",id);
        return orderMapper.getById(id);
    }

    @Override
    public List<OrderListItemVO> list() {
        log.debug("开始处理查询订单列表的请求");
        return orderMapper.list();
    }
}
